package com.hafu.dao;

import com.hafu.model.GysInfo;
import com.hafu.util.DbUtil;
import com.hafu.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class GysInfoDao {
    public Boolean addGysInfo(GysInfo gys) {
        // 修改了SQL语句中的占位符分隔符
        String sql = "insert into gysinfo(name, tel) values(?, ?)";

        Connection conn = DbUtil.getConn();
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, gys.getName());
            preparedStatement.setString(2, gys.getTel());
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }


//删除供应商信息
    public Boolean deleteGysInfo(int id){
        String sql= "delete from gysinfo where id=?";

        Connection conn = DbUtil.getConn();
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, String.valueOf(id));
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                System.out.println("删除成功");
                return true;
            } else return false;
        }
        catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return false;
    }
//修改供应商信息
    public Boolean updateGysInfo(GysInfo gys){
        String sql= "update gysinfo set name=?,tel=? where id=?";
        Connection conn = DbUtil.getConn();
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, gys.getName());
            preparedStatement.setString(2, gys.getTel());
            preparedStatement.setInt(3, gys.getId());
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                System.out.println("修改成功");
                return true;
            } else return false;
        }
        catch (Exception throwables) {
            throwables.printStackTrace();
        }

           return false;
    }
//查询供应商信息根据名字
  /*  public ArrayList <GysInfo> getGysInfoByName(GysInfo gys){
        ArrayList<GysInfo> list = new ArrayList<>();
        String sql= "select * from gysinfo";
        if (!StringUtil.isEmpty(gys.getName())){
            sql= sql+"where name like '%"+gys.getName()+"%'";
        }
        Connection conn = DbUtil.getConn();
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String tel = resultSet.getString("tel");
                GysInfo gysInfo1 = new GysInfo(id, name, tel);
                list.add(gysInfo1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;

    }*/
public ArrayList<GysInfo> getGysInfoByName(GysInfo gys){
    ArrayList<GysInfo> list = new ArrayList<>();
    String sql= "select * from gysinfo";
    if (!StringUtil.isEmpty(gys.getName())){
        sql += " where name like ?"; // 修改这里，添加空格并使用参数化查询
    }
    Connection conn = DbUtil.getConn();
    try {
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        if (!StringUtil.isEmpty(gys.getName())){
            preparedStatement.setString(1, "%" + gys.getName() + "%"); // 设置参数
        }
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String tel = resultSet.getString("tel");
            GysInfo gysInfo1 = new GysInfo(id, name, tel);
            list.add(gysInfo1);
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    return list;
}


}

